//
//  SArrayLayout.m
//  BanggoPhone
//
//  Created by Samuel on 15/8/4.
//  Copyright (c) 2015年 BG. All rights reserved.
//

#import "SArrayLayout.h"

@implementation SArrayLayout
#pragma mark -
#pragma mark ************************** 初始化
- (id)initWithTargetDataArray:(NSMutableArray*)targetDataArray;
{
    SArrayLayout *sArrayLayout = [[SArrayLayout alloc]init];
    sArrayLayout.targetDataArray = targetDataArray;
    return sArrayLayout;
}
#pragma mark -
#pragma mark ************************** 添加一个新的布局对象
- (void)addLayoutFuntionsWithName:(NSString*)layoutName
                andLayoutLocation:(NSInteger)linterger
                    andIsAbsolute:(BOOL)isBeAbsolute;
{
    if(!self.subscriptNameDictionary)
    {
        self.subscriptNameDictionary = [[NSMutableDictionary alloc]init];
        self.nameSubscriptDictionary = [[NSMutableDictionary alloc]init];
        self.AbsoluteDictionary      = [[NSMutableDictionary alloc]init];
        
    }
    if (layoutName) {
        [self.subscriptNameDictionary setObject:layoutName forKey:[NSString stringWithFormat:@"%ld",(long)linterger]];
        [self.nameSubscriptDictionary setObject:[NSString stringWithFormat:@"%ld",(long)linterger] forKey:layoutName];
        [self.AbsoluteDictionary setObject:[NSString stringWithFormat:@"%d",isBeAbsolute] forKey:layoutName];
    }
}
#pragma mark -
#pragma mark ************************** 添加一个对象
- (NSMutableArray*)getTargetDataArrayByFuntionName:(NSString*)funtionName
                                     andObjectData:(id)objectDatas
                                           andTemp:(NSInteger)temp
{
    if (![self.nameSubscriptDictionary objectForKey:funtionName]) {
        return nil;
    }
    SLayoutModel *sLayoutModel = [[SLayoutModel alloc]init];
    sLayoutModel.funtionName = funtionName;
    sLayoutModel.objectData = objectDatas;
    sLayoutModel.temp = temp;
    sLayoutModel.locationInterger = [[self.nameSubscriptDictionary objectForKey:funtionName] integerValue];
    
    BOOL isDidUpdata = NO;
    if ([[self.AbsoluteDictionary objectForKey:funtionName] boolValue]==YES) {
        isDidUpdata = [self updataTheTargetArray:sLayoutModel];
    }
    if (isDidUpdata == NO) {
        [self insertSLayoutModelToTargetArray:sLayoutModel];
    }
    return self.targetDataArray;
}
#pragma mark -
#pragma mark ************************** 更新数据
- (BOOL)updataTheTargetArray:(SLayoutModel*)sLayoutModel
{
    for (SLayoutModel *getModel in self.targetDataArray) {
        if (getModel.funtionName == sLayoutModel.funtionName) {
            getModel.objectData = sLayoutModel.objectData;
            getModel.temp = sLayoutModel.temp;
            return YES;
        }
    }
    return NO;
}
#pragma mark -
#pragma mark ************************** 数组插入排序
- (NSMutableArray*)insertSLayoutModelToTargetArray:(SLayoutModel*)sLayoutModel
{

    if ([self.targetDataArray count]==0) {
        [self.targetDataArray addObject:sLayoutModel];
        return self.targetDataArray;
    }
    /**
     *  遍历当前数组，检索是否存在和sLayoutModel相同的功能，如果存在，添加在其末尾
     */
    for (int i = (int)[self.targetDataArray count]-1; i >=0 ; i--) {
        SLayoutModel *gets = [self.targetDataArray objectAtIndex:i];
        if (gets.funtionName == sLayoutModel.funtionName) {
            [self.targetDataArray insertObject:sLayoutModel atIndex:i+1];
            return self.targetDataArray;
        }
    }
    /**
     *  上述条件：“不”存在的情况，检索布局数据里比sLayoutModel下标:sLayoutModelSubscript 优先级更低的下标（若干下标，subscriptArray），并从当前数组中遍历subscriptArray里面的数据:x（从小到大），如果存在，将sLayoutModel插入x前一位
     *
     */
    /**
     *  获取sLayoutModelSubscript
     */
    NSInteger sLayoutModelSubscript = [[self.nameSubscriptDictionary objectForKey:sLayoutModel.funtionName]integerValue];
    /**
     *  获取subscriptArray
     */
    NSMutableArray *subscriptArray = [NSMutableArray array];
    for (NSString *getKey in [self.subscriptNameDictionary allKeys]) {
        if ([getKey integerValue]>sLayoutModelSubscript) {
            [subscriptArray addObject:getKey];
        }
    }
    /**
     *  subscriptArray排序（小到大）
     */
    NSComparator cmptr = ^(id obj1, id obj2){
        if ([obj1 integerValue] > [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        
        if ([obj1 integerValue] < [obj2 integerValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    };
    NSArray *newSubscriptArray = [subscriptArray sortedArrayUsingComparator:cmptr];
    /**
     *  将target数组里的所有参数遍历，获取newSubscriptArray里最小的下标的参数的位置，将sLayoutModel插入
     */
    for (int x = 0 ; x<[newSubscriptArray count]; x++) {
        NSString *getx = [newSubscriptArray objectAtIndex:x];
        for (int tx=0; tx < [self.targetDataArray count]; tx++) {
            SLayoutModel *getModels = [self.targetDataArray objectAtIndex:tx];
            if (getModels.locationInterger == [getx integerValue]) {
                [self.targetDataArray insertObject:sLayoutModel atIndex:tx];
                return self.targetDataArray;
            }
        }
    }
    /**
     *  上述条件不成立，sLayoutModel放在最后
     */
    [self.targetDataArray addObject:sLayoutModel];
    return self.targetDataArray;
}
@end
@implementation SLayoutModel





@end
